package com.hydl.power.service.impl;

import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.ExtremeAnalyzeMapper;
import com.hydl.power.mapper.SysUserMapper;
import com.hydl.power.pojo.ExtremeAnalyze;
import com.hydl.power.pojo.SysUser;
import com.hydl.power.service.ExtremeAnalyzeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class ExtremeAnalyzeServiceImpl implements ExtremeAnalyzeService {
    @Autowired
    private ExtremeAnalyzeMapper extremeAnalyzeMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public PageInfo<ExtremeAnalyze> queryByHour(Integer pageNum, Integer pageSize, String cityId, String projectId, String roomId, String token, String startTime, String endTime) {
        SysUser sysUser =sysUserMapper.getUserByToken(token);
       if(sysUser!=null){
           if(sysUser.getStatus()==2){
               if(sysUser.getProjectId()==null){
                   sysUser.setProjectId(1);
               }
               List<ExtremeAnalyze> listfirst=extremeAnalyzeMapper.queryByHour(pageNum,pageSize,cityId,sysUser.getProjectId().toString(),roomId,startTime,endTime);
               List<ExtremeAnalyze> listent=extremeAnalyzeMapper.queryByHourEnd(cityId,sysUser.getProjectId().toString(),roomId,startTime,endTime);
               ExtremeAnalyze obj=new ExtremeAnalyze();
               for (ExtremeAnalyze extremeAnalyze : listfirst) {
                   for (ExtremeAnalyze analyze : listent) {
                       if(extremeAnalyze.getDeviceId().equals(analyze.getDeviceId()) && extremeAnalyze.getTime().substring(0,13).equals(analyze.getTime().substring(0,13))){
                           obj=analyze;
                           break;
                       }
                   }
                   System.out.println(obj.toString());
                   extremeAnalyze.setEnd_pt(obj.getEnd_pt());
                   extremeAnalyze.setEnd_power(obj.getEnd_power());
                   extremeAnalyze.setEnd_ptf(obj.getEnd_ptf());
                   extremeAnalyze.setEnd_ptn(obj.getEnd_ptn());
                   extremeAnalyze.setEnd_ia(obj.getEnd_ia());
                   extremeAnalyze.setEnd_ib(obj.getEnd_ib());
                   extremeAnalyze.setEnd_ic(obj.getEnd_ic());
                   extremeAnalyze.setEnd_ua(obj.getEnd_ua());
                   extremeAnalyze.setEnd_ub(obj.getEnd_ub());
                   extremeAnalyze.setEnd_uc(obj.getEnd_uc());
               }
               return new PageInfo<>(listfirst);
           }
       }

        List<ExtremeAnalyze> listfirst=extremeAnalyzeMapper.queryByHour(pageNum,pageSize,cityId,projectId,roomId,startTime,endTime);
        List<ExtremeAnalyze> listent=extremeAnalyzeMapper.queryByHourEnd(cityId,projectId,roomId,startTime,endTime);
        ExtremeAnalyze obj=new ExtremeAnalyze();
        for (ExtremeAnalyze extremeAnalyze : listfirst) {
            for (ExtremeAnalyze analyze : listent) {
                if(extremeAnalyze.getDeviceId().equals(analyze.getDeviceId()) && extremeAnalyze.getTime().substring(0,13).equals(analyze.getTime().substring(0,13))){
                    obj=analyze;
                    break;
                }
            }
            extremeAnalyze.setEnd_pt(obj.getEnd_pt());
            extremeAnalyze.setEnd_power(obj.getEnd_power());
            extremeAnalyze.setEnd_ptf(obj.getEnd_ptf());
            extremeAnalyze.setEnd_ptn(obj.getEnd_ptn());
            extremeAnalyze.setEnd_ia(obj.getEnd_ia());
            extremeAnalyze.setEnd_ib(obj.getEnd_ib());
            extremeAnalyze.setEnd_ic(obj.getEnd_ic());
            extremeAnalyze.setEnd_ua(obj.getEnd_ua());
            extremeAnalyze.setEnd_ub(obj.getEnd_ub());
            extremeAnalyze.setEnd_uc(obj.getEnd_uc());
        }
        return new PageInfo<>(listfirst);
    }


    @Override
    public PageInfo<ExtremeAnalyze> queryBydayStart(Integer pageNum, Integer pageSize, String cityId, String projectId, String roomId, String token, String startTime,String endTime) {
        SysUser sysUser =sysUserMapper.getUserByToken(token);
        if(sysUser!=null) {
            if (sysUser.getStatus() == 2) {
                if(sysUser.getProjectId()==null){
                    sysUser.setProjectId(1);
                }
                List<ExtremeAnalyze> listfirst=extremeAnalyzeMapper.queryBydayStart(pageNum,pageSize,cityId,sysUser.getProjectId().toString(),roomId,startTime,endTime);
                List<ExtremeAnalyze> listent=extremeAnalyzeMapper.queryBydayEnd(cityId,sysUser.getProjectId().toString(),roomId,startTime,endTime);
                ExtremeAnalyze obj=new ExtremeAnalyze();
                for (ExtremeAnalyze extremeAnalyze : listfirst) {
                    for (ExtremeAnalyze analyze : listent) {
                        analyze.setTime(analyze.getTime().substring(0,10));
                        if(extremeAnalyze.getDeviceId().equals(analyze.getDeviceId()) && extremeAnalyze.getTime().substring(0,10).equals(analyze.getTime().substring(0,10))){
                            System.out.println(analyze);
                            obj=analyze;
                            break;
                        }
                    }
                    extremeAnalyze.setTime(obj.getTime());
                    extremeAnalyze.setEnd_pt(obj.getEnd_pt());
                    extremeAnalyze.setEnd_power(obj.getEnd_power());
                    extremeAnalyze.setEnd_ptf(obj.getEnd_ptf());
                    extremeAnalyze.setEnd_ptn(obj.getEnd_ptn());
                    extremeAnalyze.setEnd_ia(obj.getEnd_ia());
                    extremeAnalyze.setEnd_ib(obj.getEnd_ib());
                    extremeAnalyze.setEnd_ic(obj.getEnd_ic());
                    extremeAnalyze.setEnd_ua(obj.getEnd_ua());
                    extremeAnalyze.setEnd_ub(obj.getEnd_ub());
                    extremeAnalyze.setEnd_uc(obj.getEnd_uc());
                }
                return new PageInfo<>(listfirst);
            }
        }
        List<ExtremeAnalyze> listfirst=extremeAnalyzeMapper.queryBydayStart(pageNum,pageSize,cityId,projectId,roomId,startTime,endTime);
        List<ExtremeAnalyze> listend=extremeAnalyzeMapper.queryBydayEnd(cityId,projectId,roomId,startTime,endTime);
        ExtremeAnalyze obj=new ExtremeAnalyze();
        for (ExtremeAnalyze extremeAnalyze : listfirst) {
            for (ExtremeAnalyze analyze : listend) {
                analyze.setTime(analyze.getTime().substring(0,10));
                if(extremeAnalyze.getDeviceId().equals(analyze.getDeviceId()) && extremeAnalyze.getTime().substring(0,10).equals(analyze.getTime().substring(0,10))){
                    obj=analyze;
                    System.out.println(analyze.toString());
                    break;
                }
            }
            extremeAnalyze.setTime(obj.getTime());
            extremeAnalyze.setEnd_pt(obj.getEnd_pt());
            extremeAnalyze.setEnd_power(obj.getEnd_power());
            extremeAnalyze.setEnd_ptf(obj.getEnd_ptf());
            extremeAnalyze.setEnd_ptn(obj.getEnd_ptn());
            extremeAnalyze.setEnd_ia(obj.getEnd_ia());
            extremeAnalyze.setEnd_ib(obj.getEnd_ib());
            extremeAnalyze.setEnd_ic(obj.getEnd_ic());
            extremeAnalyze.setEnd_ua(obj.getEnd_ua());
            extremeAnalyze.setEnd_ub(obj.getEnd_ub());
            extremeAnalyze.setEnd_uc(obj.getEnd_uc());
        }
        return new PageInfo<>(listfirst);
    }

    @Override
    public PageInfo<ExtremeAnalyze> queryByMonthStart(Integer pageNum, Integer pageSize,String cityId, String projectId, String roomId, String token, String startTime, String endTime) {
        SysUser sysUser = sysUserMapper.getUserByToken(token);
        if (sysUser != null) {
            if (sysUser.getStatus() == 2) {
                if(sysUser.getProjectId()==null){
                    sysUser.setProjectId(1);
                }
                List<ExtremeAnalyze> listfirst=extremeAnalyzeMapper.queryByMonthStart(pageNum,pageSize,cityId,sysUser.getProjectId().toString(),roomId,startTime);
                List<ExtremeAnalyze> listent=extremeAnalyzeMapper.queryByMonthEnd(cityId,sysUser.getProjectId().toString(),roomId,startTime);
                ExtremeAnalyze obj=new ExtremeAnalyze();
                for (ExtremeAnalyze extremeAnalyze : listfirst) {
                    for (ExtremeAnalyze analyze : listent) {
                        analyze.setTime(analyze.getTime().substring(0,7));
                        if(extremeAnalyze.getDeviceId().equals(analyze.getDeviceId()) && extremeAnalyze.getTime().substring(0,4).equals(analyze.getTime().substring(0,4))){
                            obj=analyze;
                            break;
                        }
                    }
                    extremeAnalyze.setTime(obj.getTime());
                    extremeAnalyze.setEnd_pt(obj.getEnd_pt());
                    extremeAnalyze.setEnd_power(obj.getEnd_power());
                    extremeAnalyze.setEnd_ptf(obj.getEnd_ptf());
                    extremeAnalyze.setEnd_ptn(obj.getEnd_ptn());
                    extremeAnalyze.setEnd_ia(obj.getEnd_ia());
                    extremeAnalyze.setEnd_ib(obj.getEnd_ib());
                    extremeAnalyze.setEnd_ic(obj.getEnd_ic());
                    extremeAnalyze.setEnd_ua(obj.getEnd_ua());
                    extremeAnalyze.setEnd_ub(obj.getEnd_ub());
                    extremeAnalyze.setEnd_uc(obj.getEnd_uc());
                }
                return new PageInfo<>(listfirst);
            }
        }
        List<ExtremeAnalyze> listfirst=extremeAnalyzeMapper.queryByMonthStart(pageNum,pageSize,cityId,projectId,roomId,startTime);
        List<ExtremeAnalyze> listent=extremeAnalyzeMapper.queryByMonthEnd(cityId,projectId,roomId,startTime);
        ExtremeAnalyze obj=new ExtremeAnalyze();
        for (ExtremeAnalyze extremeAnalyze : listfirst) {
            for (ExtremeAnalyze analyze : listent) {
                analyze.setTime(analyze.getTime().substring(0,7));
                if(extremeAnalyze.getDeviceId().equals(analyze.getDeviceId()) && extremeAnalyze.getTime().substring(0,4).equals(analyze.getTime().substring(0,4))){
                    obj=analyze;
                    break;
                }
            }
            extremeAnalyze.setTime(obj.getTime());
            extremeAnalyze.setEnd_pt(obj.getEnd_pt());
            extremeAnalyze.setEnd_power(obj.getEnd_power());
            extremeAnalyze.setEnd_ptf(obj.getEnd_ptf());
            extremeAnalyze.setEnd_ptn(obj.getEnd_ptn());
            extremeAnalyze.setEnd_ia(obj.getEnd_ia());
            extremeAnalyze.setEnd_ib(obj.getEnd_ib());
            extremeAnalyze.setEnd_ic(obj.getEnd_ic());
            extremeAnalyze.setEnd_ua(obj.getEnd_ua());
            extremeAnalyze.setEnd_ub(obj.getEnd_ub());
            extremeAnalyze.setEnd_uc(obj.getEnd_uc());
        }
        return new PageInfo<>(listfirst);
    }
}
